<?php
/**
 * @Author: Marte
 * @Date:   2017-08-03 15:11:52
 * @Last Modified by:   Marte
 * @Last Modified time: 2017-09-14 09:20:56
 */
    /**
     * undocumented function
     * $dbname 表名
     * $host    ip地址
     * $db_user 数据库帐号
     * $db_pwd  数据库密码
     * $charset 编码
     * @return string
     * @author wisdom 2017年8月3日 15:47:59
     **/
    function db_connect($dbname,$host='127.0.0.1',$db_user='root',$db_pwd='root',$charset='utf8'){
         $link = mysqli_connect('127.0.0.1', 'root', 'root', 'cms');
        //判断数据库是否连接成功
        if(!$link){
            echo '<script>alert("数据库链接失败");history.go(-1);</script>';die;
        }
        mysqli_query($link, ' set names '. $charset);
        return $link;
    }


    //增加
        function add($tab,$where){
            $k='`'.implode('`,`',array_keys($where)).'`';
            $v="'".implode("','",array_values($where))."'";
            $sql="insert into `$tab` ( $k ) values( $v )";
            mysqli_query($GLOBALS['link'],$sql);
            return mysqli_affected_rows($GLOBALS['link']);
        }
    //删除
    function del($tab,$where=array()){
        if(is_array($where)){
            $str=implode(',',$where);
            $where="in ($str)";
        }else{
            $where=' = '."'".$where."'";
        }
        $sql="delete from $tab where id $where";
        mysqli_query($GLOBALS['link'],$sql);
        return mysqli_affected_rows($GLOBALS['link']);
    }
    //修改
    function edit($tab,$where,$data){
        $file='';
        foreach ($data as $k => $v) {
            # code...
            $file.='`'.$k.'`'.'='."'".$v."'".',';

        }
        $file=substr($file,0,-1);
        $sql="update `$tab` set $file where `id`=$where";
         mysqli_query($GLOBALS['link'],$sql);
        return mysqli_affected_rows($GLOBALS['link']);
    }
    //单行查询
    function getSelectOne($tab,$where,$field='*',$is_tabs='0'){
            if(is_array($where)){
                $str='';
                foreach ($where as $k => $v) {
                    $str.=" $k = '$v' and";
                }
                if($is_tabs!=0){
                    foreach ($is_tabs as $ks => $vs) {
                        $str.=" $ks = $vs and";

                    }
            }
            $str.=' 1=1';
            $where=$str;
        }
      $sql="select $field from $tab where $where";
        //echo $sql="select $field from $tab where $where";die;
        //执行sql语句
        $result=mysqli_query($GLOBALS['link'],$sql);
        //获取变量集的数据
        $data = mysqli_fetch_assoc($result);
        return $data;
    }
    //多行查询
    function getSelect($tab,$file='*',$where='1=1',$order='',$limit='',$is_tabs='0'){
        if(!empty($order)){
            $order='order by '.$order;
        }
        if(!empty($limit)){
            $limit='limit '.$limit;
        }
        if(is_array($where)){
            $str='';
            foreach ($where as $k => $v) {
                $str.=" $k = '$v' and";
            }
            if($is_tabs!=0){
                foreach ($is_tabs as $ks => $vs) {
                    $str.=" $ks = $vs and";
                }
            }
            $str.=' 1=1';
            $where=$str;
        }
        $sql="select $file from $tab where $where $order $limit ";
                //取出数据
        $result=mysqli_query($GLOBALS['link'],$sql);
                //用while循环把数据放入到数组中去
        if(!empty($result)){
            while($arr = mysqli_fetch_assoc($result)){
                $data[]= $arr;
            }
        }
        return $data;
    }
?>